﻿Imports System.Xml
Imports System.Data.Linq
Imports System.Data.Linq.Mapping

Module Module1

    Public GlamUp As New BO.GlamUp("Data Source=OKYRPENKO-NB\SQLEXPRESS;Initial Catalog=GlamUp;User ID=glamup_user;Password=glamup")
    Sub Main()

        ImportData()
        Return
        GlamUp.Log = Console.Out
        Dim products = From c In GlamUp.Categories Group Join p In GlamUp.Products On c.categoryId Equals p.categoryId _
                        Into prodCount = Count() _
                        Select c.categoryName, prodCount

        For Each product In products
            Dim prCount As Integer = product.prodCount
            Dim catName As String = product.categoryName

        Next
    End Sub

    Sub ImportData()

        GlamUp.ExecuteCommand("delete from images")
        GlamUp.ExecuteCommand("delete from products")
        GlamUp.ExecuteCommand("delete from categories")
        GlamUp.ExecuteCommand("delete from brands")
        GlamUp.ExecuteCommand("delete from images")





        'Dim Brands = From brnd In GlamUp.Brands Select brnd
        'Dim Categories = From cat In GlamUp.Categories Select cat
        'Dim Pictures = From pict In GlamUp.Pictures Select pict
        'Dim Products = From prod In GlamUp.Products Select prod


        Dim xmlDoc As New XmlDocument
        xmlDoc.Load("D:\Projects\GlamUp\sources\jobs\Products.xml")
        For Each node As XmlNode In xmlDoc.SelectNodes("/products/product")
            Dim sku As String = node.Attributes("id").Value
            Dim brandName As String = node.Attributes("brand").Value
            Dim categoryName As String = node.Attributes("category").Value

            If (Aggregate foundProd In GlamUp.Products Where foundProd.sku = sku Into Count()) > 0 Then
                Continue For
            End If



            Dim prod As New BO.Product With { _
                .sku = sku, _
                .productName = node.Attributes("name").Value, _
                .productId = Guid.NewGuid.ToString _
            }


            ' Find related brand or create a new one



            Dim _brand = (From brnd In GlamUp.Brands _
                          Where brnd.brandName = brandName Select brnd).FirstOrDefault


            If _brand Is Nothing Then
                _brand = New BO.Brand With { _
                    .brandName = brandName, _
                    .brandId = Guid.NewGuid.ToString _
                }

                'GlamUp.Brands.InsertOnSubmit(_brand)
            End If
            prod.Brand = _brand

            ' Find related category or create a new one
            Dim _category = (From cat In GlamUp.Categories _
                          Where cat.categoryName = categoryName Select cat).FirstOrDefault

            If _category Is Nothing Then
                _category = New BO.Category With { _
                    .categoryName = categoryName, _
                    .categoryId = Guid.NewGuid.ToString _
                }
                'GlamUp.Categories.InsertOnSubmit(_category)
            End If
            prod.Category = _category


            ' Before processing images we need to insert a product to get its id




            'Set pictures 

            For Each pictNode As XmlNode In node.SelectNodes("images/image")
                prod.Images.Add( _
                    New BO.Image With { _
                        .productId = prod.productId, _
                        .isMain = Boolean.Parse(pictNode.Attributes("isMain").Value), _
                        .path = pictNode.Attributes("path").Value, _
                        .imageId = Guid.NewGuid.ToString _
                    } _
                )

            Next
            GlamUp.Log = Console.Out
            GlamUp.Products.InsertOnSubmit(prod)
            GlamUp.SubmitChanges()

        Next



        'Dim NewBrand As New Brand With {.brandName = "Max Mara"}
        'GlamUp.brands.InsertOnSubmit(NewBrand)
        'GlamUp.SubmitChanges()

    End Sub


End Module
